<template>
  <ElCard :class="s.topInfo">
    <div :class="s.top">
      <div id="left" ref="left" class="left">
        <div class="top-three">
          <div class="item">
            <div class="number">{{ total.consult_count || 0 }}</div>
            <div class="intro">咨询总数</div>
          </div>
          <div class="item two">
            <div class="number">{{ total.user_count || 0 }}</div>
            <div class="intro">会员总数</div>
          </div>
          <div class="item three">
            <div class="number">{{ total.test_count || 0 }}</div>
            <div class="intro">评测总数</div>
          </div>
        </div>
        <section ref="main" class="main"></section>
      </div>
      <div class="right">
        <div class="birthday-notice" @click="showBirthday">
          {{ birthdayNumber }}
        </div>
        <div class="today">
          <div class="item">
            <div class="number">{{ todayNum.user_count || 0 }}</div>
            <div class="intro">今日新客</div>
          </div>
          <div class="item">
            <div class="number">{{ todayNum.consult_count || 0 }}</div>
            <div class="intro">今日咨询</div>
          </div>
          <div class="item">
            <div class="number">{{ todayNum.test_count || 0 }}</div>
            <div class="intro">今日评测</div>
          </div>
        </div>
      </div>
    </div>
    <ElDialog
      :class="dialog.sourceDialog"
      :visible.sync="dialogVisible"
      width="580px"
    >
      <template>
        <ol :class="dialog.ol">
          <li v-for="(one, index) in birthdayInfo" :key="index">{{ one }}</li>
        </ol>
      </template>
      <div slot="title" :class="dialog.dialogTitle">
        <span :class="dialog.itemIcon"></span>生日提醒
      </div>
    </ElDialog>
  </ElCard>
</template>

<script>
import echarts from 'echarts';
import {http, catchAppError} from '@/util';
import url from '@/config';

export default {
  name: 'TopInfo',
  data() {
    return {
      total: {},
      todayNum: {},
      diagram: [],
      dialogVisible: false,
      birthdayNumber: 0,
      birthdayInfo: []
    };
  },
  watch: {
    diagram() {
      this.initEachrt();
    }
  },
  beforeMount() {
    http
      .post(url.total)
      .then(res => {
        this.total = res;
      })
      .catch(catchAppError);
    http
      .post(url.todayNum)
      .then(res => {
        this.todayNum = res;
      })
      .catch(catchAppError);
    http
      .post(url.birthdayReminderNum)
      .then(res => {
        this.birthdayNumber = res;
      })
      .catch(catchAppError);
    http
      .post(url.diagram)
      .then(res => {
        this.diagram = res;
      })
      .catch(catchAppError);
  },
  methods: {
    showBirthday() {
      if (!this.birthdayNumber) return;
      http
        .post(url.birthdayReminder)
        .then(res => {
          this.birthdayInfo = res;
          this.dialogVisible = true;
        })
        .catch(catchAppError);
    },
    initEachrt() {
      // 基于准备好的dom，初始化echarts实例
      this.$refs.main.style.width = `${this.$refs.left.clientWidth}px`;
      const myChart = echarts.init(this.$refs.main);

      // 指定图表的配置项和数据
      const option = {
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['咨询数', '评测数']
        },
        grid: {
          top: 30,
          bottom: 0,
          left: 20,
          right: 20,
          containLabel: true,
          show: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            splitLine: {
              show: true
            },
            data: this.diagram.map(res => res.time)
          }
        ],
        yAxis: {
          type: 'value',
          splitLine: {
            show: true
          },
          axisLine: {
            show: false
          }
        },
        series: [
          {
            name: '咨询数',
            type: 'line',
            smooth: true,
            itemStyle: {normal: {color: '#73e7c4'}},
            lineStyle: {normal: {color: '#73e7c4'}},
            areaStyle: {
              normal: {
                color: '#98eed4'
              }
            },
            data: this.diagram.map(res => res.consult_count)
          },
          {
            name: '评测数',
            type: 'line',
            smooth: true,
            areaStyle: {
              normal: {
                color: '#6fddce'
              }
            },
            lineStyle: {normal: {color: '#4ec5f8'}},
            itemStyle: {normal: {color: '#4ec5f8'}},
            data: this.diagram.map(res => res.test_count)
          }
        ]
      };
      myChart.setOption(option);
    }
  }
};
</script>

<style lang="scss" module="s">
.top-info {
  :global {
    margin-bottom: 20px;
    .el-card__body {
      padding: 12px 0 30px;
    }
  }
  .top {
    :global {
      display: flex;
      .left {
        flex-grow: 3.521;
        width: 352.1px;
        border-right: 1px solid #ebebeb;
        .main {
          width: 600px;
          height: 160px;
          margin-top: 40px;
        }
        .top-three {
          display: flex;
          padding-top: 18px;
          padding-left: 30px;
          .item {
            flex-grow: 1;
            padding-left: 45px;
            text-align: center;
            height: 45px;
            box-sizing: border-box;
            background-image: url(../../assets/img/main-icon1.png);
            background-repeat: no-repeat;
            &.two {
              background-position: 0 -82px;
            }
            &.three {
              background-position: 0 -163px;
            }
            .intro,
            .number {
              width: 80px;
            }
            .intro {
              font-size: 16px;
            }
            .number {
              margin-bottom: 8px;
              font-size: 20px;
              color: #4b90ff;
            }
          }
        }
      }
      .right {
        flex-grow: 1;
        width: 100px;
        padding-top: 8px;
        .birthday-notice {
          cursor: pointer;
          font-size: 48px;
          width: 156px;
          box-sizing: border-box;
          text-align: center;
          margin: 0 auto 50px;
          height: 70px;
          line-height: 70px;
          padding-right: 12px;
          padding-left: 88px;
          color: #fff;
          background-image: url(../../assets/img/main-icon1.png);
          box-shadow: 0 0 14px rgba($color: #4b90ff, $alpha: 0.5);
          background-position: 0 -266px;
          background-size: 100%;
          background-repeat: no-repeat;
        }
        .today {
          width: 170px;
          margin-left: auto;
          margin-right: auto;
          .item {
            padding-left: 40px;
            height: 44px;
            border-bottom: 1px solid #f7f7f7;
            background-image: url(../../assets/img/main-icon1.png);
            background-repeat: no-repeat;
            text-indent: 20px;
            position: relative;
            &:after {
              content: '';
              position: absolute;
              left: 0;
              bottom: 0;
              height: 1px;
              width: 40px;
              background-color: #29bb9b;
            }
            &:nth-child(1) {
              background-position: 0 -378px;
            }
            &:nth-child(2) {
              background-position: 0 -422px;
            }
            &:nth-child(3) {
              background-position: 0 -469px;
            }
            .number {
              color: #4b90ff;
              font-size: 16px;
              padding-top: 6px;
            }
            .intro {
              font-size: 16px;
            }
          }
        }
      }
    }
  }
}
</style>

<style lang="scss" module="dialog">
@import '@/assets/css/dialog.scss';
.ol {
  margin-top: 5px;
  padding-left: 30px;
  max-height: 350px;
  overflow-y: auto;
  :global {
    li {
      margin-bottom: 20px;
    }
  }
}
</style>
